package com.synjones.beijing
{
    import mx.controls.Alert;
    import mx.rpc.IResponder;
    import mx.rpc.events.FaultEvent;
    import com.synjones.beijing.controls.IconAlert;
    
    public class TokenResponder implements IResponder
    {
        private var _resultHandler:Function;
        private var _faultTitle:String;
        private var _showAlert:Boolean
        private var _faultHandler:Function;
        private var _returnFaultEvent:Boolean;


        //public function TokenResponder(result:Function, fault:Function)
        //{
        //    super();
        //    resultHandler = result;
        //    faultHandler = fault;
        //}
        
        public function TokenResponder(
            result:Function, 
            faultTitle:String=null,
            showAlert:Boolean = true,
            returnFaultEvent:Boolean = false,
            faultFunction:Function=null)
        {
            super();
            _resultHandler = result;
            _faultTitle = faultTitle;
            _returnFaultEvent = returnFaultEvent;
            _showAlert = showAlert;
            
            if(_returnFaultEvent)
                _faultHandler = faultFunction;
        }
        public function result(data:Object):void
        {
            _resultHandler(data);
        }
    
        public function fault(info:Object):void
        {
            if(_showAlert){
                IconAlert.showError(FaultEvent(info).fault.toString(), _faultTitle);
            }
            if((_returnFaultEvent) && (_faultHandler != null)){
                _faultHandler(info);
            }
        }
    
    }
}